package com.yingxin.yxerp.module.sys.controller;

import com.yingxin.yxerp.module.sys.entity.Salesman;
import com.yingxin.yxerp.module.sys.entity.builder.SalesmanBuilder;
import com.yingxin.yxerp.module.sys.service.ISalesmanService;
import com.yingxin.yxerp.module.sys.vo.req.*;
import com.yingxin.yxerp.module.sys.vo.resp.SalesmanRespVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 业务员 前端控制器
 * @author yuebao
 * @since 2025-03-31
 */

@RestController
@RequestMapping("/sys/salesman")
public class SalesmanController {

    @Autowired
    private ISalesmanService salesmanService;

    @ApiOperation(value = "查询列表-全部")
    @PostMapping("/list")
    public List<SalesmanRespVo> list(@RequestBody SalesmanQueryVo queryVo) {
        return salesmanService.listVo( queryVo );
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public SalesmanRespVo get(@PathVariable("id") String id) {
        return salesmanService.getVo( id );
    }

    @PreAuthorize("hasAuthority('system:salesman:save')")
    @ApiOperation(value = "保存")
    @PostMapping("/save")
    public String save(@RequestBody @Valid SalesmanCreateVo createVo) {
        Salesman salesman = SalesmanBuilder.build( createVo );
        salesmanService.create( salesman );
        return salesman.getId();
    }

    @PreAuthorize("hasAuthority('system:salesman:delete')")
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public void delete(@PathVariable String id) {
        salesmanService.delete( id );
    }

    @PreAuthorize("hasAuthority('system:salesman:update')")
    @ApiOperation(value = "编辑")
    @PutMapping("/update")
    public void update(@RequestBody @Valid SalesmanUpdateVo updateVo) {
        Salesman salesman = new Salesman();
        BeanUtils.copyProperties( updateVo, salesman );
        salesmanService.update( salesman );
    }

}
